﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.DataLayer;
using QuanLyDoanVien.Controller;
using QuanLyDoanVien.General;
using DevComponents.DotNetBar.Controls;
using System.IO;
namespace QuanLyDoanVien
{
    public partial class frmQLHS : DevComponents.DotNetBar.Office2007Form
    {
        List<LopInfo> ds_lop = new List<LopInfo>();
        List<HocSinhInfo> ds_hs = new List<HocSinhInfo>();
        HocSinhCtrl m_HocSinhCtrl = new HocSinhCtrl();
        QuyDinh m_QuyDinh = new QuyDinh();
        LopCtrl m_LopCtrl=new LopCtrl();
        ChucVuCtrl m_ChucVuCtrl = new ChucVuCtrl();
        public static ComboBoxEx cbx_DSHS;
        public static ComboBoxEx cbx_ChucVu;
        OpenFileDialog open;
        public static String root_img_link = "./hinhhocsinh/";
        public frmQLHS()
        {
            InitializeComponent();
            cbx_DSHS = this.cbx_HS_MaLop;
            cbx_ChucVu = this.cbx_HS_MaCv;
            DataService.OpenConnection();
        }
        
        private void Load_DS_HocSinh()
        {
            dataGridView_HS.Rows.Clear();
            ds_hs = m_HocSinhCtrl.layDSHS();
            foreach (HocSinhInfo hs in ds_hs)
            {
                dataGridView_HS.Rows.Add(hs.MaHocSinh,hs.HoTen,hs.MaLop,hs.GioiTinh,hs.NgaySinh,hs.NoiSinh,hs.NgayVaoDoan,hs.MaChucVu,
                    hs.HoTenCha,hs.HoTenMe,hs.SoDTCha,hs.SoDTMe,hs.TuoiCha,hs.TuoiMe,hs.NgheCha,hs.NgheMe);
            }
        }
        private void Load_DS_HS_Lop(String ml)
        {
            dataGridView_HS.Rows.Clear();
            ds_hs = m_HocSinhCtrl.layHocSinhTheoLop(ml);
            foreach (HocSinhInfo hs in ds_hs)
            {
                dataGridView_HS.Rows.Add(hs.MaHocSinh, hs.HoTen, hs.MaLop, hs.GioiTinh, hs.NgaySinh, hs.NoiSinh, hs.NgayVaoDoan, hs.MaChucVu,
                    hs.HoTenCha, hs.HoTenMe, hs.SoDTCha, hs.SoDTMe, hs.TuoiCha, hs.TuoiMe, hs.NgheCha, hs.NgheMe);
            }
        }
        private void Refresh_HocSinh()
        {
            this.txt_HS_MaHs.Text ="";
            this.txt_HS_TenHs.Text = "";
            this.txt_HS_MaHs.Text = "";
            this.txt_HS_TenCha.Text = "";
            this.txt_HS_TenMe.Text = "";
            this.txt_HS_DtHs.Text = "";
            this.txt_HS_DtCha.Text = "";
            this.txt_HS_DtMe.Text = "";
            this.txt_HS_Email.Text = "";
            this.txt_HS_THCS.Text = "";
            this.txt_HS_NS_Ngay.Text = "";
            this.txt_HS_NS_Thang.Text = "";
            this.txt_HS_NS_Nam.Text = "";
            this.txt_TuoiCha.Text = "";
            this.txt_Tuoime.Text = "";
            this.txt_HS_NoiSinh.Text = "";
            this.txt_HS_NVD_ngay.Text = "";
            this.txt_HS_NVD_thang.Text = "";
            this.txt_HS_NVD_nam.Text = "";
            this.txt_HS_NgheCha.Text = "";
            this.txt_HS_NgheMe.Text = "";
            this.txt_HS_NhanXet.Text = "";
            this.cbx_HS_MaLop.SelectedIndex = -1;
            this.cbx_HS_GioiTinh.SelectedIndex = -1;
            this.cbx_HS_MaCv.SelectedIndex = -1;
            this.chx_Doanvien.Checked = false;
            this.chx_Ratruong.Checked = false;

            
        }
        private void btn_HS_Xoa_Click(object sender, EventArgs e)
        {
            m_HocSinhCtrl.XoaHocSinh(txt_HS_MaHs.Text);
            Load_DS_HocSinh();
        }

        private void btn_HS_Luu_Click(object sender, EventArgs e)
        {
           // DataRow rHS= m_HocSinhCtrl.LayThongTin1HS(txt_HS_MaHs.Text);
            String namsinh = this.txt_HS_NS_Ngay.Text + "/" + this.txt_HS_NS_Thang.Text + "/" + this.txt_HS_NS_Nam.Text;
            String ngayvaodoan = this.txt_HS_NVD_ngay.Text + "/" + this.txt_HS_NVD_thang.Text + "/" + this.txt_HS_NVD_nam.Text;
            int dv = 0;
            int rt = 0;
            int idLop = 0;
            int idGT = 0;
            int idCV = 0;

            if (cbx_HS_MaLop.SelectedIndex > 0)
            {
                idLop = cbx_HS_MaLop.SelectedIndex;
            }
            if (cbx_HS_GioiTinh.SelectedIndex > 0)
            {
                idGT = cbx_HS_GioiTinh.SelectedIndex;
            }
            if (cbx_HS_MaCv.SelectedIndex > 0)
            {
                idCV = cbx_HS_MaCv.SelectedIndex;
            }
            if (chx_Doanvien.Checked == true)
            {
                dv = 1;
            }
            if (chx_Ratruong.Checked == true)
            {
                rt = 1;
            }
           
            HocSinhInfo hs = new HocSinhInfo(
                this.txt_HS_MaHs.Text,
                this.txt_HS_TenHs.Text,//2
                this.cbx_HS_MaLop.SelectedValue.ToString(),
                namsinh,//4
                ngayvaodoan,//5
                this.txt_HS_TenCha.Text,
                this.txt_HS_TenMe.Text,
                this.txt_HS_DtCha.Text,
                this.txt_HS_DtMe.Text,
                this.txt_HS_DtHs.Text,
                int.Parse(this.txt_TuoiCha.Text),
                int.Parse(this.txt_Tuoime.Text), //12
                this.txt_HS_NgheCha.Text,
                this.txt_HS_NgheMe.Text,
                this.txt_HS_Email.Text,
                this.txt_HS_NoiSinh.Text,//16
                this.cbx_HS_GioiTinh.Items[idGT].ToString(),
                this.txt_HS_THCS.Text,
                this.cbx_HS_MaCv.SelectedValue.ToString(),
                0,//20
                this.txt_HS_NhanXet.Text,
                this.txt_linkanh.Text,
                rt,
                    dv //24
                );
            m_HocSinhCtrl.SuaHocSinh(hs);
            Load_DS_HocSinh();

        }
        private HocSinhInfo New_HocSinh()
        {
            //String mahs = m_HocSinhCtrl.getAutoID();
            String namsinh = this.txt_HS_NS_Ngay.Text + "/" + this.txt_HS_NS_Thang.Text + "/" + this.txt_HS_NS_Nam.Text;
            String ngayvaodoan = this.txt_HS_NVD_ngay.Text + "/" + this.txt_HS_NVD_thang.Text + "/" + this.txt_HS_NVD_nam.Text;
            int dv=0;
            int rt = 0;
            int idLop = 0;
            int idGT = 0;
            int idCV = 0;
    
            if (cbx_HS_MaLop.SelectedIndex > 0)
            {
                idLop = cbx_HS_MaLop.SelectedIndex;
            }
            if (cbx_HS_GioiTinh.SelectedIndex > 0)
            {
                idGT = cbx_HS_GioiTinh.SelectedIndex;
            }
            if (cbx_HS_MaCv.SelectedIndex > 0)
            {
                idCV = cbx_HS_MaCv.SelectedIndex;
            }
            if (chx_Doanvien.Checked == true)
            {
                dv = 1;
            }
            if (chx_Ratruong.Checked == true)
            {
                rt = 1;
            }
            //FileInfo fi = new FileInfo(txt_linkanh.Text);
            HocSinhInfo hs = new HocSinhInfo(
                            this.txt_HS_MaHs.Text, //1
                           this.txt_HS_TenHs.Text,//2
                           this.cbx_HS_MaLop.SelectedValue.ToString(),
                           namsinh,//4
                           ngayvaodoan,//5
                           this.txt_HS_TenCha.Text,
                           this.txt_HS_TenMe.Text,
                           this.txt_HS_DtCha.Text,
                           this.txt_HS_DtMe.Text,
                           this.txt_HS_DtHs.Text,
                           int.Parse(this.txt_TuoiCha.Text),
                           int.Parse(this.txt_Tuoime.Text), //12
                           this.txt_HS_NgheCha.Text,
                           this.txt_HS_NgheMe.Text,
                           this.txt_HS_Email.Text,
                           this.txt_HS_NoiSinh.Text,//16
                           this.cbx_HS_GioiTinh.Items[idGT].ToString(),
                           this.txt_HS_THCS.Text,
                           this.cbx_HS_MaCv.SelectedValue.ToString(),
                           0,//20
                           this.txt_HS_NhanXet.Text,
                           txt_linkanh.Text,
                            rt,
                             dv //24
                           );
           
            return hs;
        }
        private bool check_DuLieuNhap()
        {
            int tCha, tMe;
            if (int.TryParse(txt_TuoiCha.Text, out tCha) == false)
            {
                MessageBoxEx.Show("Tuổi Cha Chưa Đúng ! Làm Ơn Nhập Lại : ", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;

            }
            if (int.TryParse(txt_Tuoime.Text, out tMe) == false)
            {
                MessageBoxEx.Show("Tuổi Mẹ Chưa Đúng ! Làm Ơn Nhập Lại : ", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;

            }
            if (m_HocSinhCtrl.layTenHocSinh(txt_HS_MaHs.Text) != null)
            {
                MessageBoxEx.Show(" Mã Học Sinh Đã Tồn Tại ! ", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
            if(txt_HS_MaHs.Text=="")
            {
                MessageBoxEx.Show(" Mã Học Sinh Không Được Để Trống ! ", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
            return true;

        }
        private void btn_HS_Them_Click(object sender, EventArgs e)
        {

            if(check_DuLieuNhap()==false)
                return;
            HocSinhInfo hs = New_HocSinh();
            m_HocSinhCtrl.ThemHocSinh(hs);
            Load_DS_HocSinh();
            
        }

        private void frmQLHS_Load(object sender, EventArgs e)
        {
            m_LopCtrl.HienThiComboBox(this.cbx_HS_MaLop);
            m_ChucVuCtrl.HienThiComboBox(this.cbx_HS_MaCv);
            Load_DS_HocSinh();

           
        }

        private void bnt_linkanh_Click(object sender, EventArgs e)
        {

            try
            {
                // open file dialog
                open = new OpenFileDialog();
                // image filters
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";

                if (open.ShowDialog() == DialogResult.OK)
                {
                    //   copy to img folder in debug
                    txt_linkanh.Text =open.SafeFileName;
                    FileInfo fi = new FileInfo(open.FileName);
                    String link_img = root_img_link + open.SafeFileName;
                    if(!File.Exists(link_img))
                        fi.CopyTo(link_img, true);
                    // display image in picture box
                    try
                    {
                        picAvatar.Image = new Bitmap(link_img);
                    }
                    catch { }
                    // image file path
                   
                }
                
            }
            catch (Exception)
            {
                throw new ApplicationException("Load Ảnh Lỗi !!!! ....");
            }
            
        }

        private void btn_HS_LamMoi_Click(object sender, EventArgs e)
        {
           
            Load_DS_HocSinh();
            Refresh_HocSinh();
            
        }

        private void dataGridView_HS_SelectionChanged(object sender, EventArgs e)
        {
            int index;
            if (dataGridView_HS.SelectedRows.Count <= 0 || dataGridView_HS.SelectedRows[0].Index == dataGridView_HS.RowCount - 1)
            {
                return;
            }
            else
            {
                index = dataGridView_HS.SelectedRows[0].Index;
            }
           DataGridViewRow dr = dataGridView_HS.Rows[index];



           String[] ns = { "0", "0", "0" };
           String[] nvd = { "0", "0", "0" };
           DataRow row = m_HocSinhCtrl.LayThongTin1HS(dr.Cells["MaHS"].Value.ToString());
           if (Convert.ToString(row["NgaySinh"]).Length > 6)
               ns = Convert.ToString(row["NgaySinh"]).Split('/');
           if (Convert.ToString(row["NgayVaoDoan"]).Length > 6)
               nvd = Convert.ToString(row["NgayVaoDoan"]).Split('/');
           this.txt_HS_MaHs.Text = dr.Cells["MaHS"].Value.ToString();
           this.txt_HS_TenHs.Text = Convert.ToString(row["HoTen"]);
           this.txt_HS_TenCha.Text = Convert.ToString(row["HoTenCha"]);
           this.txt_HS_TenMe.Text = Convert.ToString(row["HoTenMe"]);
           this.txt_HS_DtHs.Text = Convert.ToString(row["SoDTHS"]);
           this.txt_HS_DtCha.Text = Convert.ToString(row["SoDTMe"]);
           this.txt_HS_DtMe.Text = Convert.ToString(row["SoDTCha"]);
           this.txt_HS_Email.Text = Convert.ToString(row["Email"]);
           this.txt_HS_THCS.Text = Convert.ToString(row["THCS"]);
           this.txt_HS_NS_Ngay.Text = ns[0];
           this.txt_HS_NS_Thang.Text = ns[1];
           this.txt_HS_NS_Nam.Text = ns[2];
           this.txt_TuoiCha.Text = Convert.ToString(row["TuoiCha"]);
           this.txt_Tuoime.Text = Convert.ToString(row["TuoiMe"]);
           this.txt_HS_NoiSinh.Text = Convert.ToString(row["NoiSinh"]);
           this.txt_HS_NVD_ngay.Text = nvd[0];
           this.txt_HS_NVD_thang.Text = nvd[1];
           this.txt_HS_NVD_nam.Text = nvd[2];
           this.txt_HS_NgheCha.Text = Convert.ToString(row["NgheCha"]);
           this.txt_HS_NgheMe.Text = Convert.ToString(row["NgheMe"]);
           this.txt_HS_NhanXet.Text = Convert.ToString(row["NhanXet"]);



           // control dac biet
           // Bitmap anhhs = new Bitmap(Convert.ToString(row["AnhHocSinh"]));
           this.txt_linkanh.Text = Convert.ToString(row["AnhHocSinh"]);
            if (Convert.ToString(row["AnhHocSinh"]) != "")
            {
                try
                {
                    String link_img =root_img_link+ Convert.ToString(row["AnhHocSinh"]);
                    this.picAvatar.Image =new  Bitmap(link_img);
                }
                catch { }
            }
            else
            {
                try
                {
                    this.picAvatar.Image = Properties.Resources.no_avatar;
                }
                catch { }
 
            }
           
           this.cbx_HS_MaLop.SelectedValue = Convert.ToString(row["MaLop"]);
           try
           {
               if (Convert.ToString(row["GioiTinh"])=="Nam")
                this.cbx_HS_GioiTinh.SelectedIndex = 0;
               else
                   this.cbx_HS_GioiTinh.SelectedIndex =1;
           }
           catch { }
           try
           {
               this.cbx_HS_MaCv.SelectedValue = Convert.ToString(row["MaChucVu"]);
           }
           catch { }
           try
           {
               if (Convert.ToInt32(row["DoanVien"]) == 1)
                   this.chx_Doanvien.Checked = true;
               else
                   this.chx_Doanvien.Checked = false;

               if (Convert.ToInt32(row["RaTruong"]) == 1)
                   this.chx_Ratruong.Checked = true;
               else
                   this.chx_Ratruong.Checked = false;
           }
           catch { }

        }

     

        private void bntTimLop_Click(object sender, EventArgs e)
        {
            try
            {
                Load_DS_HS_Lop(this.cbx_HS_MaLop.SelectedValue.ToString());
            }
            catch { }
        }

    }
}